package com.rav;

import com.rav.widget.R;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.media.AudioManager;
import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;

public class RingAndVibrate extends Activity {
	
	protected BroadcastReceiver m_ringerModeReceiver = null;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        RadioGroup menu = (RadioGroup) findViewById(R.id.menu);
        menu.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				
				switch(checkedId) {
					case R.id.onlyRing:
						setOnlyRing();
						break;
	
					case R.id.onlyVibrate:
						setOnlyVibrate();
						break;
	
					case R.id.ringAndVibrate:
						setRingAndVibrate();
						break;
	
					case R.id.silent:
						setSilent();
						break;
				} 
				
			}
		});
    }
    
    @Override
	public void onResume() {
		super.onResume();
		
		// update radio-group to reflect changed ringer-mode
		updateRadioGroup();
		
		// register receiver to check for ringer-mode updates while running
		if (m_ringerModeReceiver == null) {
			m_ringerModeReceiver = new BroadcastReceiver() {
				@Override
				public void onReceive(Context context, Intent intent) {
			        updateRadioGroup();
				}
			};
			registerReceiver(m_ringerModeReceiver, new IntentFilter(AudioManager.RINGER_MODE_CHANGED_ACTION));
		}
	}
    
    protected void updateRadioGroup() {
    	int checkedId = current();
    	RadioButton buttonChecked = (RadioButton) findViewById(checkedId);
    	buttonChecked.setChecked(true);
    }
    
    protected void setOnlyRing() {
    	AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
		audioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
		audioManager.setVibrateSetting(AudioManager.VIBRATE_TYPE_RINGER, AudioManager.VIBRATE_SETTING_OFF);
		audioManager.setVibrateSetting(AudioManager.VIBRATE_TYPE_NOTIFICATION, AudioManager.VIBRATE_SETTING_OFF);
    }
    
    protected void setOnlyVibrate() {
    	AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
		audioManager.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
		audioManager.setVibrateSetting(AudioManager.VIBRATE_TYPE_RINGER, AudioManager.VIBRATE_SETTING_ON);
		audioManager.setVibrateSetting(AudioManager.VIBRATE_TYPE_NOTIFICATION, AudioManager.VIBRATE_SETTING_ON);
    }
    
    protected void setRingAndVibrate() {
    	AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
		audioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
		audioManager.setVibrateSetting(AudioManager.VIBRATE_TYPE_RINGER, AudioManager.VIBRATE_SETTING_ON);
		audioManager.setVibrateSetting(AudioManager.VIBRATE_TYPE_NOTIFICATION, AudioManager.VIBRATE_SETTING_ON);
    }
    
    protected void setSilent() {
    	AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
		audioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
		audioManager.setVibrateSetting(AudioManager.VIBRATE_TYPE_RINGER, AudioManager.VIBRATE_SETTING_OFF);
		audioManager.setVibrateSetting(AudioManager.VIBRATE_TYPE_NOTIFICATION, AudioManager.VIBRATE_SETTING_OFF);
    }
    
    protected int current() {
		AudioManager audio = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
		
		switch (audio.getRingerMode()) {
			case AudioManager.RINGER_MODE_SILENT: 
				return R.id.silent;
			case AudioManager.RINGER_MODE_VIBRATE: 
				return R.id.onlyVibrate;
		}
		
		if (audio.shouldVibrate(AudioManager.VIBRATE_TYPE_RINGER))		
			return R.id.ringAndVibrate;
		
		return R.id.onlyRing;
    }
}